#include "ftpserver.h"

FtpServer::FtpServer(QObject *parent) : QTcpServer(parent)
{
    connect(this, SIGNAL(acceptError(QAbstractSocket::SocketError)),
            this, SLOT(onAcceptError(QAbstractSocket::SocketError)));
}

FtpServer::~FtpServer()
{
    stop();
}

void FtpServer::setConfig(const ServerConfig &config)
{
    m_serverConfig = config;
}

bool FtpServer::start()
{
    return listen(QHostAddress::AnyIPv4, 21);
}

void FtpServer::stop()
{
    close();
    qDeleteAll(m_sessions);
}

void FtpServer::onAcceptError(QAbstractSocket::SocketError error)
{
    qDebug() << "accept error" << errorString();
}

void FtpServer::incomingConnection(qintptr handle)
{
    FtpSession *session = new FtpSession(handle, m_serverConfig, this);
    m_sessions.append(session);
}
